using FSM.IDal; 
using FSM.IBll; 
using FSM.Model; 
using SqlSugar; 
using System; 
using System.Collections.Generic; 
using System.Text;
using Util.Helper;
using System.Linq;
using Util.Extensions;

namespace FSM.Bll 
{ 
    public class RelMenuActionBll: IRelMenuActionBll 
    { 
        #region 模块 
        private readonly IRelMenuActionDal dal;
        public RelMenuActionBll(IRelMenuActionDal dal) 
        { 
            this.dal = dal;
        } 
        #endregion 
 
        #region 查询 
 
        /// <summary> 
        /// 根据键值查询单个信息 
        /// </summary> 
        /// <param name="pkid">键值</param> 
        /// <returns></returns> 
        public rel_menu_action Get(string pkid) 
        { 
            return dal.GetById(pkid); 
        } 
 
        /// <summary> 
        /// 查询批量信息 
        /// </summary> 
        /// <param name="menu_no">菜单编号</param> 
        /// <param name="act_no">动作编号</param> 
        /// <param name="page">页码</param> 
        /// <param name="page_size">每页显示的条数</param> 
        /// <returns></returns> 
        public Tuple<List<rel_menu_action>, int> List(string menu_no, string act_no, int page, int page_size) 
        { 
            ISugarQueryable<rel_menu_action> queryable = dal.CreateQueryable(); 
            queryable = queryable.WhereIF(!string.IsNullOrEmpty(menu_no),p => p.menu_no == menu_no); 
            queryable = queryable.WhereIF(!string.IsNullOrEmpty(act_no),p => p.act_no == act_no); 
             
            PageModel pm = new PageModel 
            { 
                PageIndex = page, 
                PageSize = page_size 
            }; 
            Tuple<List<rel_menu_action>, int> result = dal.GetPageListAndTotal(queryable, pm);
            return result; 
        }

        /// <summary>
        /// 通过菜单编号获得其拥有的动作信息
        /// </summary>
        /// <param name="menu_no">菜单编号</param>
        /// <returns></returns>
        public List<sys_action> ListByMenuNo(string menu_no)
        {
            return dal.ListActionsByMenuNo(menu_no);
        }
 
        #endregion 
 
        #region 提交 
 
        /// <summary> 
        /// 添加 
        /// </summary> 
        /// <param name="model">实体</param> 
        /// <returns></returns> 
        public bool Insert(rel_menu_action model) 
        { 
            model.create_date = DateTime.Now; 
            return dal.Insert(model); 
        }

        /// <summary>
        /// 添加当前列表数据
        /// </summary>
        /// <param name="oper">操作人员</param>
        /// <param name="menu_no">菜单编号</param>
        /// <param name="act_nos">动作编号列表</param>
        /// <returns></returns>
        public bool BatchInsert(string oper, string menu_no, List<string> act_nos)
        {
            dal.BeginTran();
            try
            {
                oper.ValidEmptyThrowException("操作人员");
                menu_no.ValidEmptyThrowException("要操作的菜单");

                DateTime dtNow = DateTime.Now;
                //先查询出原先拥有的动作信息
                List<rel_menu_action> menuActions = dal.GetList(p => p.menu_no == menu_no);
                IEnumerable<string> oldActNos = menuActions.Select(p => p.act_no);
                IEnumerable<string> pkids = menuActions.Where(p => act_nos.Contains(p.act_no) == false).Select(p => p.pkid);
                bool success = false;
                if (pkids.Count() > 0)
                {
                    success = dal.Delete(pkids.ToArray());
                    if (success == false)
                    {
                        throw new Exception("移除原有动作失败");
                    }
                }

                List<rel_menu_action> models = new List<rel_menu_action>();
                IEnumerable<string> newActNos = act_nos.Where(p => oldActNos.Contains(p) == false).Select(p => p);
                if (newActNos.Count() > 0)
                {
                    foreach (string act_no in newActNos)
                    {
                        rel_menu_action model = new rel_menu_action()
                        {
                            pkid = Guid.NewGuid().ToString("N"),
                            creater = oper,
                            create_date = dtNow,
                            act_no = act_no,
                            menu_no = menu_no
                        };
                        models.Add(model);
                    }
                }

                if (models.Count() > 0)
                {
                    success = dal.Insert(models);
                    if (!success)
                    {
                        throw new Exception("批量插入数据异常");
                    }
                }
                dal.CommitTran();
                return true;
            }
            catch (Exception)
            {
                dal.RollbackTran();
                throw;
            }
        }

        /// <summary> 
        ///  删除 
        /// </summary> 
        /// <param name="pkid">键值</param> 
        /// <returns></returns> 
        public bool Delete(params string[] pkid_arr) 
        { 
            return dal.Delete(pkid_arr); 
        } 
        #endregion 
    } 
} 
